'''
自动订餐，先登录oa，再调用订餐
'''
from time import time
from lxml import etree
from urllib import parse
import requests

    
base_url ="https://oa.kedacom.com"

# 先登录oa，拿到html
session = requests.Session()
resp = session.get(base_url)

html = etree.HTML(resp.text)

# 得到action
login_action = html.xpath("//form[@id='fm1']/@action[1]")

print(login_action[0])
# 这个是登录的url
login_url = parse.urljoin("https://sso.kedacom.com:8443",login_action[0])

loginTickets = html.xpath("//form[@id='fm1']//input[@name='loginTicket']/@value")

executions = html.xpath("//form[@id='fm1']//input[@name='execution']/@value")

login_ticket = loginTickets[0]
execution = executions[0]
#print(login_ticket)
#print(execution)

# print(login_url)
# 再拿到登录需要的其他东西
data = {
    "username":"wuqiang@kedacom.com",
    "password":"Wu415439280",
    "loginTicket":login_ticket,
    "execution":execution,
    "kdmoaAccount":"nice",
    "loginType":"web",
    "_eventId":"submit",
    "rememberMetest":True
}

#print(f"login_url = {login_url},data = {data}")

# 登录 OA
session.post(login_url,data=data)
# print(f"----{response.status_code}")

# 登录之后，调用订餐的url进行订餐
order_dinner_url = parse.urljoin(base_url,"/kmoa/mealOrder/orderOrCancel.do")

print(time)

# 得到当天 
order_data= {
    "checkTime":"2024年06月24日",
    "clientType":"PC",
    "isOrder":0,
    "userEmail":"wuqiang@kedacom.com"
}

resp = session.post(order_dinner_url,data=order_data)
print(f"订餐的返回结果{resp.text}")

